﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
    <meta content="width=device-width, user-scalable=no" name="viewport"/>
    <meta content="telephone=no" name="format-detection"/>
    <title></title>
    <script src="echarts.min.js"></script>
    <script src="jquery.min.js"></script>
    <style>

        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .left_right {
            width: 100%;
            overflow: hidden;
            font-size: 4.5vw;
            text-align: center;
            line-height: 12vw;
            background-color: #f8f8f8;
        }

        .left_right .left {
            float: left;
            width: 100%;
        }

        .left_content {
            overflow: hidden
        }

        .left_content .content_div {
            overflow: hidden;
            float: left;
            margin-bottom: 7%;
        }

        .left_content .content_div .content_div_title {
            text-align: center;
            font-size: 4vw;
            margin-top: 20px;
            height: 25px;
            color: #606060;
        }

        .left_content .content_div .content_div_pie {
            width: 25vw;
            height: 35vw;
        }


    </style>
    <script>
        window.onload = function () {
            var content ="[{\"precent\":0.0,\"productCategory\":\"金典奶\",\"salesAmount\":11348.64},{\"precent\":0.0,\"productCategory\":\"味可滋\",\"salesAmount\":391.5},{\"precent\":0.0,\"productCategory\":\"谷粒多\",\"salesAmount\":843.48},{\"precent\":0.0,\"productCategory\":\"基础优酸乳\",\"salesAmount\":1503.06},{\"precent\":0.0,\"productCategory\":\"儿童成长奶\",\"salesAmount\":642.5},{\"precent\":0.0,\"productCategory\":\"儿童花色奶\",\"salesAmount\":222.92},{\"precent\":0.0,\"productCategory\":\"安慕希\",\"salesAmount\":1614.65},{\"precent\":0.0,\"productCategory\":\"砖优酸乳\",\"salesAmount\":418.38},{\"precent\":0.0,\"productCategory\":\"QQ星有机奶\",\"salesAmount\":337.49},{\"precent\":0.0,\"productCategory\":\"舒化奶\",\"salesAmount\":0.0}]";
            JavaCallJs(content);
        }


    </script>
</head>
<body>
<div class="left_content" id="leftId"></div>
</body>
<script type="text/javascript">
    var myChart;

    function JavaCallJs(content) {
        //设置左边图表数据
        $("#leftId").empty();
        var div = document.createElement("div");
        div.id = "newDivLeft";
        div.className = 'content_div';
        div.style.width = document.documentElement.clientWidth + 'px';

        //图表标题div
        <!--        var div1 = document.createElement("div");-->
        <!--        div1.id = "new1DivLeft";-->
        <!--        div1.className = 'content_div_title';-->
        <!--        div1.innerHTML = "";-->
        <!--        div1.style.fontSize = "larger";-->
        <!--        div1.style.color = "black";-->
        <!--        div.appendChild(div1);-->

        //图表div
        var div2 = document.createElement("div");
        div2.id = "new2DivLeft";
        div2.className = 'content_div_pie';
        div2.style.width = document.documentElement.clientWidth + 'px';
        div2.style.height = document.documentElement.clientHeight + 'px';

        var contentObj = JSON.parse(content);
        var selectControl = document.createElement("select");
        selectControl.id = "select_DataItems";
        selectControl.style.width = "100px";
        selectControl.style.height = "20px";
        selectControl.style.position = "absolute";
        selectControl.style.top = "1px";
        selectControl.style.right = "1px";
        selectControl.style.zIndex = "100";
        selectControl.style.backgroundColor="#fff";
        selectControl.style.border="0px solid #fff";
        selectControl.style.outline = "none";
        $(selectControl).on("change", function () {
            var index = $(selectControl).val()
            myChart.dispatchAction(
                {
                    type: 'showTip',
                    seriesIndex: 0,
                    dataIndex: index
                });
        });
        for (i = 0; i < contentObj.length; i++) {
            var selectItem = document.createElement("option");

            selectItem.innerText = contentObj[i].productCategory;
            selectItem.value = i;
            selectControl.appendChild(selectItem);
        }
        div.appendChild(selectControl);
        div.appendChild(div2);

        //设置圆形图表
        setChart(content, div2);
        $("#leftId").append(div);
    }

    function accAdd(arg1, arg2) {
        var r1, r2, m;
        try {
            r1 = arg1.toString().split(".")[1].length
        } catch (e) {
            r1 = 0
        }
        try {
            r2 = arg2.toString().split(".")[1].length
        } catch (e) {
            r2 = 0
        }
        m = Math.pow(10, Math.max(r1, r2))
        return (arg1 * m + arg2 * m) / m
    }

    function setChart(item, div) {
        myChart = echarts.init(div);
        var json = JSON.parse(item);
        var datas = [];
        var sum = 0
        for (var i = 0; i < json.length; i++) {
            var json1 = {};
            json1.name = json[i].productCategory;
            var salesAmount = json[i].salesAmount;
            sum = accAdd(sum, salesAmount)
            if (salesAmount < 0) {
                salesAmount = 0;
            }
            json1.value = salesAmount;
            datas.push(json1);
            
        }
        var option = {

            title: [
                {
                    text: '销量总额\n' + Math.round(sum) + '元',
                    left: 'center',
                    top: '45%',
                    textStyle: { //主标题文本样式{"fontSize": 18,"fontWeight": "bolder","color": "#333"}
                        fontSize: 12,
                        fontWeight: 'normal',
                    },
                },
                {
                    text: '当月产品\n销售额',
                    left: 'center',
                    bottom: 0,
                    textStyle: { //主标题文本样式{"fontSize": 18,"fontWeight": "bolder","color": "#333"}
                        fontSize: 12,
                        fontWeight: 'normal',
                    },
                },
            ],
            legend: {
                show: false,
            },
            tooltip: {
                confine: true,
                trigger: 'item',
                fontSize: 10,
                formatter: "{b} :<br/>{c}元<br/>[占比：{d}%]"
            },
            series: [
                {
                    color: ['#91cc75', '#fac858', '#c99979', '#73c0de',
                        '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc',
                        '#54211d', '#f15a22', '#ffe600', '#228fbd',
                        '#b54334', '#6f60aa', '#ea66a6', '#40835e'],
                    type: 'pie',
                    avoidLabelOverlap: true,
                    hoverAnimation: false, // 取消掉环形图鼠标移上去时自动放大
                    radius: ['60%', '80%'],
                    center:['50%','48%'],
                    label: {
                        show: false,
                    },
                    emphasis: {
                        label: {
                            show: false
                        },
                    },
                    labelLine: {
                        show: false
                    },
                    data: datas
                }
            ]
        };
        myChart.setOption(option);

    }


</script>

</html>